package com.sinopec.basemodule.common;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.sinopec.basemodule.AppConstants;
import com.sinopec.basemodule.R;
import com.sinopec.basemodule.base.BaseActivity;
import com.sinopec.basemodule.base.CustomToolbar;

/**
 * Created by :Ronny
 * Date on :2018/3/23 22:03
 */

public class WebViewActivity extends BaseActivity {
    public static final int TO_PROTOCOL = 0;
    public static final int TO_INVOICE = 1;
    public static final int TO_ADDRESS = 2;
    public static final int TO_PRIVACY_PROTOCOL = 4;
    int action;

    private ProgressBar progressBar;
    WebView webview;
    private String intentUrl = "http://www.zhaoguanche.com/";

    //通知已读状态
    private boolean isread;
    private String noticeWebUrl;

    @Override
    public int getContentView() {
        return R.layout.activity_webview;
    }

    @Override
    public void configToolbar(CustomToolbar rToolbar) {
        Intent intent = getIntent();
        action = intent.getIntExtra(AppConstants.WEB_ACTION, -1);

        //通知网页链接
        noticeWebUrl = intent.getStringExtra(AppConstants.NOTICE_WEBURL);
        isread = intent.getBooleanExtra(AppConstants.ISREAD, true);
        String title = "";
        switch (action) {
            case TO_PROTOCOL:
                title = "用户服务协议";
                intentUrl = getIntent().getStringExtra(AppConstants.WEB_URL);
                break;
            case TO_PRIVACY_PROTOCOL:
                title = "用户隐私政策";
                intentUrl = getIntent().getStringExtra(AppConstants.WEB_URL);
                break;
            case -1:
                showMessage("打开web页面失败");
                finish();

                return;
        }
        if (intentUrl == null) {
            showMessage("打开web页面失败");
            finish();
            return;
        }
        rToolbar.setTitle(title);
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        progressBar = findViewById(R.id.progressBar);
        webview = findViewById(R.id.webview);
        webview.setVerticalScrollBarEnabled(false); //垂直滚动不显示
        WebSettings settings = webview.getSettings();
        settings.setSupportZoom(true);
        //设置支持js
        settings.setJavaScriptEnabled(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);
        settings.setTextSize(WebSettings.TextSize.NORMAL);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webview.loadUrl(intentUrl);
        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    progressBar.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    progressBar.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    progressBar.setProgress(newProgress);//设置进度值
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
